<?php
/**
 * params表管理
 * 2013-03-14 by huanran.zhao
 */
class ParamsApp extends CommonApp{

	public function indexAct(){
		//table
		$table = My::arr2var($_GET,'table');
		if(My::is_var($table) AND My::is_table($table) AND My::is_table($table.'_params')){//主表 及 副表都存在时
			//列表
			$where = '1 ';
			if(My::is_id(My::arr2var($_GET,'id')))
				$where .= "AND `id`=".$_GET['id']." ";
			if(My::is_var(My::arr2var($_GET,'key')))
				$where .= "AND `key` like '".$_GET['key']."%' ";
			if(My::arr2var($_GET,'value')!='')
				$where .= "AND `value` like '%".I('get.value')."%' ";
			if(My::is_id(My::arr2var($_GET,'idid')))
				$where .= "AND `idid`=".$_GET['idid']." ";
			$page = My::page(DB($table.'_params')->where($where)->count());
			$result = DB($table.'_params')->where($where)->order('`idid`')->limit($page['limits'])->select();
			$this->assign('result',$result);
			$this->assign('page',$page);
			$this->assign('table',$table);//主表
		}
		//主表或副表不存在时
		elseif($table!='' AND (!My::is_table($table) OR !My::is_table($table.'_params'))){
			$this->assign('error',"{$table} 主表或副表不存在！");
		}
		//print_r($result);
		$this->display();
	}

	/**
	 * 清理无用数据
	 */
	public function clearAct(){
		set_time_limit(0);
		$table = My::arr2var($_GET,'table');
		if(!My::is_table($table))
			$this->ajaxReturn(404,"要清理的主表名不存在!");
		$page = I('get.page',0);
		$count = DB($table.'_params')->field('id')->count();
		if($count/10<$page)
			$this->ajaxReturn(404,'数据清理完成!');
		$str = '';
		$ids = array();
		//每10条数据为一页
		$result = DB($table.'_params')->limit(($page*10).','.($page*10)+10)->select();
		if(is_array($result))
		foreach($result as $key=>$value){
			//Runtime::start();
			//过滤当前数据对应的表中的idid是否存在,如果不存在,将其删除
			if(!DB($table)->field('id')->where("id={$value['idid']}")->find()){
				$ids[] = $value['id'];
				$str.="表:{$table} 不存在该id数据:({$value['idid']}),已成功删除该数据<br>";
				continue;
			}
			//Runtime::stop();
			//echo Runtime::spent().'---'.$key.'<br>';
		}
		//开删
		$ids = implode(',',$ids);
		if($ids!='')
			DB($table.'_params')->where("`id` IN({$ids})")->delete();
		$this->assign('page',$page+1);
		$this->assign('str',$str);
		$this->display();
	}

	/**
	 * 重新索引
	 */
	public function indexingAct(){
		//索引操作
		if(My::arr2var($_GET,'type')=='todo'){
			$tablename = My::arr2var($_POST,'tablename');
			if(!My::is_table($tablename))
				$this->ajaxReturn(300,"主表不存在({$tablename})!");
			$fields = DB($tablename)->getDbFields();
			if(!in_array('item',$fields))
				$this->ajaxReturn(300,'表中不存在item字段!');
			elseif(!in_array('params',$fields))
				$this->ajaxReturn(300,'表中不存在params字段!');
			
			set_time_limit(0);
			$page = My::is_id(My::arr2var($_REQUEST,'page'))?$_REQUEST['page']:1;
			$count = DB($tablename)->field('id')->cache()->count();
			if($page>1 AND (10*($page-1)>=$count))
				$this->ajaxReturn(400,'全部索引完成!');
			//每10条数据为一页
			$limits = (($page-1)*10).",10";
			$result = DB($tablename)->limit($limits)->select();
			if(is_array($result))
			foreach($result as $value){
				$value['params'] = My::decodeJson($value['params']);
				My::setParams($tablename,$value['params'],$value['id'],$value['item']);
			}
			$persent = intval(($page+1)*10/$count*100);
			if($persent>100) $persent = 100;
			$this->ajaxReturn($persent.'%',"索引处理中,请耐心等候...{$persent}%<br>");
		}
		//显示模板
		else
			$this->display();
	}

}